<?php
/*
  $Id: configuration.php,v 1.43 2003/06/29 22:50:51 hpdl Exp $

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/

	require('includes/application_top.php');
	
	// Order Box
	$orders_contents = "";
	$orders_status_query = tep_db_query("select orders_status_name, orders_status_id from " . TABLE_ORDERS_STATUS . " where language_id = '" . $languages_id . "'");
	while ($orders_status = tep_db_fetch_array($orders_status_query)) {
		$orders_pending_query = tep_db_query("select count(*) as count from " . TABLE_ORDERS . " where orders_status = '" . $orders_status['orders_status_id'] . "'");
		$orders_pending = tep_db_fetch_array($orders_pending_query);
		$orders_contents .= '<a href="' . tep_href_link(FILENAME_ORDERS, 'selected_box=customers&status=' . $orders_status['orders_status_id']) . '">' . $orders_status['orders_status_name'] . '</a>: ' . $orders_pending['count'] . '<br>';
	}
	$orders_contents = substr($orders_contents, 0, -4);
	
	// Statistics Box
	$customers_query = tep_db_query("select count(*) as count from " . TABLE_CUSTOMERS);
	$customers = tep_db_fetch_array($customers_query);
	$products_query = tep_db_query("select count(*) as count from " . TABLE_PRODUCTS . " where products_status = '1'");
	$products = tep_db_fetch_array($products_query);
	$reviews_query = tep_db_query("select count(*) as count from " . TABLE_REVIEWS);
	$reviews = tep_db_fetch_array($reviews_query);	
  
?>
<!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
<html <?php echo HTML_PARAMS; ?>>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>">
<title><?php echo TITLE; ?></title>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<script language="javascript" src="includes/general.js"></script>
</head>
<body marginwidth="0" marginheight="0" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0" bgcolor="#FFFFFF">
<!-- header //-->
<?php
  require(DIR_WS_INCLUDES . 'header.php');
?>
<!-- header_eof //-->
<!-- body //-->
<table border="0" width="100%" cellspacing="2" cellpadding="2">
  <tr>
    <td width="<?php echo BOX_WIDTH; ?>" valign="top">
    <table border="0" width="<?php echo BOX_WIDTH; ?>" cellspacing="1" cellpadding="1" class="columnLeft">
        <!-- left_navigation //-->
        <?php require(DIR_WS_INCLUDES . 'column_left.php'); ?>
        <!-- left_navigation_eof //-->
      </table></td>
    <!-- body_text //-->
    <td width="100%" valign="top">
    <table width="100%" border="0" cellspacing="0" cellpadding="2">
          <tr>
        <td width="100%" colspan="2"><table border="0" width="100%" cellspacing="0" cellpadding="0">
          <tr>
            <td class="pageHeading"><?php echo HEADING_TITLE; ?></td>
            <td class="pageHeading" align="right"><?php echo tep_draw_separator('pixel_trans.gif', 1, HEADING_IMAGE_HEIGHT); ?></td>
            <td class="pageHeading" align="right">&nbsp;</td>
          </tr>
        </table></td>
      </tr>
        <tr>
          <td align="left" valign="top" width="25%" rowspan="3">
          <!-- orders table //-->
            <table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="headerBar">
                <td class="headerBarContent"><?php echo BOX_TITLE_ORDERS; ?></td>
              </tr>
              <tr class="infoBoxContent">
                <td class="smallText"><?php
	echo $orders_contents;

?>
                </td>
              </tr>
            </table>
<?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?>
            <!-- statistics table //-->
            <table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="headerBar">
                <td class="headerBarContent"><?php echo BOX_TITLE_STATISTICS; ?></td>
              </tr>
              <tr class="infoBoxContent">
                <td class="smallText"><?php
	echo BOX_ENTRY_CUSTOMERS . ' ' . $customers['count'] . '<br>';
	echo BOX_ENTRY_PRODUCTS . ' ' . $products['count'] . '<br>';
	echo BOX_ENTRY_REVIEWS . ' ' . $reviews['count'] . '<br><br><br>';?>
                </td>
              </tr>
            </table><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?>
            <!-- top customers table //-->
            <table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="headerBar">
                <td class="headerBarContent"><?php echo BOX_TITLE_CUSTOMERS; ?></td>
              </tr>
              <tr class="infoBoxContent">
                <td class="smallText"><table border="0" width="100%" cellspacing="0" cellpadding="2">
                    <tr class="dataTableHeadingRow">
                      <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_CUSTOMERS; ?></td>
                      <td class="dataTableHeadingContent" align="right"><?php echo TABLE_HEADING_TOTAL_PURCHASED; ?>&nbsp;</td>
                    </tr>
                    <?php
	$customers_query_raw = "select c.customers_firstname, 
								   c.customers_lastname, 
								   sum(op.products_quantity * op.final_price) as ordersum 
								   from " . TABLE_CUSTOMERS . " c, 
								   " . TABLE_ORDERS_PRODUCTS . " op, 
								   " . TABLE_ORDERS . " o 
								   where c.customers_id = o.customers_id 
								   and o.orders_id = op.orders_id 
								   group by c.customers_firstname, c.customers_lastname 
								   order by ordersum DESC LIMIT 5";

	$customers_query = tep_db_query($customers_query_raw);
	$count = 0;
	$cnt = 1;
	while ($customers = tep_db_fetch_array($customers_query)) {
		$count++;
?>
                    <tr bgcolor="<?php echo ((++$cnt)%2==0) ? '#e0e0e0' : '#ffffff' ?>">
                      <td class="dataTableContent"><?php echo '<a href="' . tep_href_link(FILENAME_CUSTOMERS, 'search=' . $customers['customers_lastname'], 'NONSSL') . '">' . $customers['customers_firstname'] . ' ' . $customers['customers_lastname'] . '</a>'; ?></td>
                      <td class="dataTableContent" align="right"><?php echo $currencies->format($customers['ordersum']); ?>&nbsp;</td>
                    </tr>
                    <?php
	}
	
	if ($count < 1) {
		echo "<tr><td colspan=\"2\" class=\"smallText\">No products have been ordered yet.</td></tr>";
	}	
?>
                  </table></td>
              </tr>
            </table></td>
          <td align="left" valign="top" width="25%" rowspan="3">
<!-- sales table //-->
            <table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="headerBar">
                <td class="headerBarContent"><?php echo BOX_TITLE_SALES; ?></td>
              </tr>
              <tr class="infoBoxContent">
                <td class="smallText"><?php
	require(DIR_WS_CLASSES . 'currencies.php');
	$currencies = new currencies();
	
	$sales_query_raw = "select ot.value
								   from " . TABLE_ORDERS . " o, 
								   " . TABLE_ORDERS_TOTAL . " ot 
								   where o.orders_id = ot.orders_id  
								   and ot.class = 'ot_subtotal' 
								   and o.date_purchased >= '" . date("Y-m-d") . " 00:00:00' 
								   and o.date_purchased <= '" . date("Y-m-d") . " 23:59:59' 
";

	$sales_query = tep_db_query($sales_query_raw);
	while ($sales = tep_db_fetch_array($sales_query)) {
		$dailyTotal = $dailyTotal + $sales['value'];
	}
		
	$sales_query_raw = "select ot.value
								   from " . TABLE_ORDERS . " o, 
								   " . TABLE_ORDERS_TOTAL . " ot 
								   where o.orders_id = ot.orders_id  
								   and ot.class = 'ot_subtotal' 
								   and o.date_purchased >= '" . date("Y-m") . "-01' 
								   and o.date_purchased <= '" . date("Y-m") . "-31' 
";

	$sales_query = tep_db_query($sales_query_raw);
	while ($sales = tep_db_fetch_array($sales_query)) {
		$monthlyTotal = $monthlyTotal + $sales['value'];
	}

	$sales_query_raw = "select ot.value
								   from " . TABLE_ORDERS . " o, 
								   " . TABLE_ORDERS_TOTAL . " ot 
								   where o.orders_id = ot.orders_id  
								   and ot.class = 'ot_subtotal' 
								   and o.date_purchased >= '" . date("Y-01") . "-01' 
								   and o.date_purchased <= '" . date("Y-12") . "-31' 
";

	$sales_query = tep_db_query($sales_query_raw);
	while ($sales = tep_db_fetch_array($sales_query)) {
		$yearlyTotal = $yearlyTotal + $sales['value'];
	}	
	echo "Today: " . $currencies->format($dailyTotal) . "<br>";
	echo "This Month: " . $currencies->format($monthlyTotal) . "<br>";
	echo "This Year: " . $currencies->format($yearlyTotal) . "<br><br>";
?>
                </td>
              </tr>
            </table><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?>
<!-- top sellers table //-->
            <table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="headerBar">
                <td class="headerBarContent"><?php echo BOX_TITLE_TOP_SELLERS; ?></td>
              </tr>
              <tr class="infoBoxContent">
                <td class="smallText"><table border="0" width="100%" cellspacing="0" cellpadding="2">
                    <tr class="dataTableHeadingRow">
                      <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_MODEL; ?></td>
                      <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_PRODUCTS; ?></td>
                      <td class="dataTableHeadingContent" align="center"><?php echo TABLE_HEADING_PURCHASED; ?>&nbsp;</td>
                    </tr>
                    <?php
     $products_query_raw = "select op.products_id, 
	 							   op.products_model, 
								   op.products_name, 
								   sum(op.products_quantity) as quantitysum, 
								   sum(op.products_price*op.products_quantity) as gross 
								   FROM " . TABLE_ORDERS . " as o, 
								   " . TABLE_ORDERS_PRODUCTS . " AS op 
								   WHERE o.date_purchased BETWEEN '" . date("Y-m") . "-01 00:00:00' 
								   AND '" . date("Y-m") . "-31 23:59:59' 
								   AND o.orders_id = op.orders_id 
								   GROUP BY op.products_id 
								   ORDER BY quantitysum DESC, 
								   op.products_model LIMIT 5";

	$products_query = tep_db_query($products_query_raw);
	$count = 0;
	$cnt = 1;
	while ($products = tep_db_fetch_array($products_query)) {
		$count++;	
?>
                    <tr bgcolor="<?php echo ((++$cnt)%2==0) ? '#e0e0e0' : '#ffffff' ?>">
                      <td class="dataTableContent"><?php echo $products['products_model']; ?>
                      <td class="dataTableContent"><?php echo '<a href="' . tep_href_link(FILENAME_CATEGORIES, 'action=new_product_preview&read=only&pID=' . $products['products_id'] . '&origin=' . FILENAME_STATS_PRODUCTS_PURCHASED . '?page=' . $HTTP_GET_VARS['page'], 'NONSSL') . '">' . $products['products_name'] . '</a>'; ?></td>
                      <td class="dataTableContent" align="center"><?php echo $products['quantitysum']; ?>&nbsp;</td>
                    </tr>
                    <?php
	}
	
	if ($count < 1) {
		echo "<tr><td colspan=\"3\" class=\"smallText\">No products have been sold this month.</td></tr>";
	}
?>
                  </table></td>
              </tr>
            </table><?php echo tep_draw_separator('pixel_trans.gif', '1', '5'); ?>
<!-- top viewed table //-->
            <table border="0" width="100%" cellspacing="0" cellpadding="2">
              <tr class="headerBar">
                <td class="headerBarContent"><?php echo BOX_TITLE_TOP_VIEWED; ?></td>
              </tr>
              <tr class="infoBoxContent">
                <td class="smallText"><table border="0" width="100%" cellspacing="0" cellpadding="2">
                    <tr class="dataTableHeadingRow">
                      <td class="dataTableHeadingContent"><?php echo TABLE_HEADING_PRODUCTS; ?></td>
                      <td class="dataTableHeadingContent" align="center"><?php echo TABLE_HEADING_VIEWED; ?></td>
                    </tr>
                    <?php
	$products_query_raw = "select p.products_id, 
  								  pd.products_name, 
								  pd.products_viewed, 
								  l.name from " . TABLE_PRODUCTS . " p, 
								  " . TABLE_PRODUCTS_DESCRIPTION . " pd, 
								  " . TABLE_LANGUAGES . " l 
								  where p.products_id = pd.products_id and l.languages_id = pd.language_id 
								  LIMIT 5";
	$products_query = tep_db_query($products_query_raw);
	$count = 0;
	$cnt = 1;
	while ($products = tep_db_fetch_array($products_query)) {
		$count++;
?>
                    <tr bgcolor="<?php echo ((++$cnt)%2==0) ? '#e0e0e0' : '#ffffff' ?>">
                      <td class="dataTableContent"><?php echo '<a href="' . tep_href_link(FILENAME_CATEGORIES, 'action=new_product_preview&read=only&pID=' . $products['products_id'] . '&origin=' . FILENAME_STATS_PRODUCTS_VIEWED . '?page=' . $HTTP_GET_VARS['page'], 'NONSSL') . '">' . $products['products_name'] . '</a> (' . $products['name'] . ')'; ?></td>
                      <td class="dataTableContent" align="center"><?php echo $products['products_viewed']; ?>&nbsp;</td>
                    </tr>
                    <?php
	}
	if ($count < 1) {
		echo "<tr><td colspan=\"2\" class=\"smallText\">No products have been sold yet.</td></tr>";
	}			
	
?>
                  </table></td>
              </tr>
            </table></td>
        </tr>
      </table></td>
    <!-- body_text_eof //-->
  </tr>
</table>
<!-- body_eof //-->
<!-- footer //-->
<?php require(DIR_WS_INCLUDES . 'footer.php'); ?>
<!-- footer_eof //-->
<br>
</body>
</html>
<?php require(DIR_WS_INCLUDES . 'application_bottom.php'); ?>
